# import psutil
import os
import pyfits
# import beam_weighting as wg
import sys

import numpy as np

from bak import TimeCal


class FitsMerge:

    def __init__(self, fits1, fits2, flag, accio,tempfdata):
        if (flag == 1 and (fits1.telecope != fits2.telecope)):
            print 'can not merge  fits  from diff telescope'
            sys.exit()

        self.telecope = fits1.telecope
        self.tmp=[fits1,fits2,fits1]

       # if self.telecope == 'FAST':


        #if (self.telecope == 'PARKES'):



            # nline=np.size(temp_float_dat_scl)/2/nchan




  #  def modifyPHeader(self, startfreq, endfreq, startfreqNetNum, endfreqNetNum,dec):
        # print startfreq,endfreq,'--'
      #  if (self.telecope == 'FAST'):



       # if (self.telecope == 'PARKES'):


    # @profile

    def merge(self):
        #self.tmp[2]=self.tmp[0]
        self.tmp[2].float_tsubint_a = np.concatenate(
            (np.array(self.tmp[0].data1['TSUBINT']), np.array(self.tmp[1].data1['TSUBINT'])))
        self.tmp[2].float_offs_sub_a = np.concatenate(
            (np.array(self.tmp[0].data1['OFFS_SUB']), np.array(self.tmp[1].data1['OFFS_SUB'])))

        self.tmp[2].float_lst_sub_a = np.concatenate(
            (np.array(self.tmp[0].data1['LST_SUB']), np.array(self.tmp[1].data1['LST_SUB'])))
        self.tmp[2].float_ra_sub_a = np.concatenate(
            (np.array(self.tmp[0].data1['RA_SUB']), np.array(self.tmp[1].data1['RA_SUB'])))
        self.tmp[2].float_dec_sub_a = np.concatenate(
            (np.array(self.tmp[0].data1['DEC_SUB']), np.array(self.tmp[1].data1['DEC_SUB'])))
        self.tmp[2].float_glon_sub_a = np.concatenate(
            (np.array(self.tmp[0].data1['GLON_SUB']), np.array(self.tmp[1].data1['GLON_SUB'])))
        self.tmp[2].float_glat_sub_a = np.concatenate(
            (np.array(self.tmp[0].data1['GLAT_SUB']), np.array(self.tmp[1].data1['GLAT_SUB'])))
        self.tmp[2].float_fd_ang_a = np.concatenate(
            (np.array(self.tmp[0].data1['FD_ANG']), np.array(self.tmp[1].data1['FD_ANG'])))
        self.tmp[2].float_pos_ang_a = np.concatenate(
            (np.array(self.tmp[0].data1['POS_ANG']), np.array(self.tmp[1].data1['POS_ANG'])))
        self.tmp[2].float_par_ang_a = np.concatenate(
            (np.array(self.tmp[0].data1['PAR_ANG']), np.array(self.tmp[1].data1['PAR_ANG'])))
        self.tmp[2].float_tel_az_a = np.concatenate(
            (np.array(self.tmp[0].data1['TEL_AZ']), np.array(self.tmp[1].data1['TEL_AZ'])))
        self.tmp[2].float_tel_zen_a = np.concatenate(
            (np.array(self.tmp[0].data1['TEL_ZEN']), np.array(self.tmp[1].data1['TEL_ZEN'])))

        self.tmp[2].float_dat_freq_a = np.array(self.tmp[0].data1['DAT_FREQ'])

        self.tmp[2].float_dat_wts_a = np.array(self.tmp[0].data1['DAT_WTS'])

        self.tmp[2].float_dat_offs_a = np.concatenate((self.tmp[0].data1['DAT_OFFS'], self.tmp[1].data1['DAT_OFFS']))

        self.tmp[2].float_dat_scl_a = np.concatenate((self.tmp[0].data1['DAT_SCL'], self.tmp[1].data1['DAT_SCL']))



        self.tmp[2].nsubint = int(self.tmp[0].header1['NAXIS2']) + int(self.tmp[1].header1['NAXIS2'])
        # totalTime(s)
        self.tmp[2].timePerSubint = self.tmp[0].float_tsubint_a[0]
        # Sample time (s)
        self.tmp[2].sampleTime = self.tmp[0].header1['TBIN']
        # Nr of polarisations
        self.tmp[2].Npolae = self.tmp[0].header1['NPOL']

        self.tmp[2].totalTime = self.tmp[0].timePerSubint * self.tmp[2].nsubint
        self.tmp[2].numSubint=self.tmp[2].totalTime/self.tmp[2].timePerSubint
        self.tmp[2].chnum=self.tmp[2].nchan
        self.tmp[2].chanBW = self.tmp[0].header1['CHAN_BW']
        self.tmp[2].float_data= np.concatenate((self.tmp[0].float_data, self.tmp[1].float_data))
        self.tmp[2].float_data2=self.tmp[2].float_data

        self.tmp[2].float_tsubint = self.tmp[2].float_tsubint_a
        self.tmp[2].float_offs_sub= self.tmp[2].float_offs_sub_a

        self.tmp[2].float_lst_sub = self.tmp[2].float_lst_sub_a
        self.tmp[2].float_ra_sub = self.tmp[2].float_ra_sub_a
        self.tmp[2].float_dec_sub = self.tmp[2].float_dec_sub_a
        self.tmp[2].float_glon_sub = self.tmp[2].float_glon_sub_a
        self.tmp[2].float_glat_sub = self.tmp[2].float_glat_sub_a
        self.tmp[2].float_fd_ang = self.tmp[2].float_fd_ang_a
        self.tmp[2].float_pos_ang = self.tmp[2].float_pos_ang_a
        self.tmp[2].float_par_ang =  self.tmp[2].float_par_ang_a
        self.tmp[2].float_tel_az = self.tmp[2].float_tel_az_a
        self.tmp[2].float_tel_zen = self.tmp[2].float_tel_zen_a
        self.tmp[2].float_dat_freq = self.tmp[2].float_dat_freq_a
        self.tmp[2].float_dat_wts = self.tmp[2].float_dat_wts_a
        self.tmp[2].float_dat_offs = self.tmp[2].float_dat_offs_a
        self.tmp[2].float_dat_scl = self.tmp[2].float_dat_scl_a
        self.tmp[2].nsuboffs_a=self.tmp[2].nsuboffs
        return self.tmp[2]
        #self.tmp[2].float_data_1 = tempfdata
        #self.tmp[2].float_data_2 = self.tmp[1].data1['DATA']








        #
        #
        # self.float_data=np.concatenate((self.float_data_1,self.float_data_2),axis=3)
        #
        # self.float_dat_freq = np.concatenate((self.float_dat_freq_a_1,self.float_dat_freq_a_2),axis=1)
        #
        # self.float_dat_wts = np.concatenate((self.float_dat_wts_a_1,self.float_dat_wts_a_2),axis=1)
        #
        # self.float_dat_freq = np.concatenate((self.float_dat_freq_a_1,self.float_dat_freq_a_2),axis=1)
        #
        # self.float_dat_wts = np.concatenate((self.float_dat_wts_a_1,self.float_dat_wts_a_2),axis=1)




